﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MemberPoint.Entity.POCOModels;
using MemberPoint.Entity.ViewModels.ShopViewModel;
using MemberPoint.Common;
using MemberPoint.Business;

namespace MemberPoint.Web.Controllers.Shops
{
    public class ShopController : Controller
    {
        /// <summary>
        /// 跳转列表页面
        /// </summary>
        /// <returns></returns>
        #region
        public ActionResult Index()
        {
            return View();
        }
        #endregion

        /// <summary>
        /// 显示列表信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        #region
        public ActionResult GetShopsList(ShopsConditionViewModel model)
        {
            //实例化ShopService类
            ShopService db = new ShopService();
            //获取ShopService中返回的值
            var dataGrid = db.GetShops(model);
            //将返回值中的data转为Json
            return Json(dataGrid.Data);
        }
        #endregion
        
        /// <summary>
        /// 添加、修改页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        #region
        public ActionResult Edit(int id = 0)
        {
            //下拉列表显示信息
            var shopTypeList = EnumHelper.EnumListDic<CategoryTypeEnum>("全部", "0");
            var shopTypeSelectList = new SelectList(shopTypeList, "value", "key");
            ViewBag.ShopType = shopTypeSelectList;

            using (Member db = new Member()) 
            {
                //判断id是否>0
                if (id > 0)
                {
                    //查询该id的信息
                    var model = db.Shops.FirstOrDefault(s => s.S_ID == id);
                    if (model != null)
                    {
                        //如果信息不为空，显示信息
                        var viewmodel = new CreateShopViewModel()
                        {
                            S_ID = model.S_ID,
                            S_Address = model.S_Address,
                            S_Category = model.S_Category,
                            S_ContactName = model.S_ContactName,
                            S_ContactTel = model.S_ContactTel,
                            S_Name = model.S_Name,
                            S_Remark = model.S_Remark
                        };
                        //返回视图，并将店铺信息返回
                        return View(viewmodel);
                    }
                    else
                    {
                        //如果信息为空，输出没有该店铺信息
                        return Content("没有该店铺信息");
                    }
                }
                else
                {
                    //id<0跳转添加页面
                    return View();
                }
            }
        }
        #endregion
        
        /// <summary>
        /// 添加店铺方法
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        #region
        public ActionResult CreateShop(CreateShopViewModel model)
        {
            try
            {
                ShopService db = new ShopService();
                return Json(db.Add(model));
            }
            catch
            {
                return View("Edit");
            }
        }
        #endregion
        
        /// <summary>
        /// 修改店铺方法
        /// </summary>
        /// <param name="id"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        #region
        public ActionResult EditShop(int id, CreateShopViewModel model)
        {
            try
            {
                ShopService db = new ShopService();
                return Json(db.Update(model));
            }
            catch
            {
                return View("Index");
            }
        }
        #endregion
        
        /// <summary>
        /// 删除店铺方法
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        #region
        public ActionResult Delete(int id)
        {
            try
            {
                ShopService db = new ShopService();
                //返回Json类型的信息
                return Json(db.Delete(id));
            }
            catch
            {
                return View("Index");
            }
        }
        #endregion
    }
}